﻿using Microsoft.SystemCenter.Orchestrator.Integration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenerateReports
{
    public class ReportBase
    {
        protected GlobalConfig config;

        [ActivityConfiguration, ActivityOutput]
        public GlobalConfig Config
        {
            get
            {
                return config;
            }
            set
            {
                config = value;
            }
        }

        public List<Dictionary<string, string>> GetFailedActivities(string RunbookId)
        {
            List<Dictionary<string, string>> result = new List<Dictionary<string, string>>();
            string url;
            string webresponse;
            string runbookname;

            // get name of runbook
            url = config.ServiceUrl;
            url += "Runbooks?$filter=Id eq Guid'" + RunbookId + "'";
            webresponse = Util.GetServiceData(url);
            runbookname = Util.FindPropertyValue(webresponse, "Name");

            // get each instances Id of given runbook
            url = config.ServiceUrl;
            url += "RunbookInstances?$filter=RunbookId eq Guid'" + RunbookId + "'";
            url += "and Status eq 'warning'";
            webresponse = Util.GetServiceData(url);
            List<string> riid = Util.FindPropertyValueCollection(webresponse, "Id");

            // get all activity Id's for each runbook instance with warning/failure
            List<List<string>> aiidlist = new List<List<string>>();
            foreach (var id in riid)
            {
                url = config.ServiceUrl;
                url += "ActivityInstances?$filter=RunbookInstanceId eq Guid'" + id + "'";
                url += "and Status eq 'failed'";
                webresponse = Util.GetServiceData(url);
                var tmp = Util.FindEntityPropertyAll(webresponse);
                foreach (var d in tmp)
                {
                    url = config.ServiceUrl;
                    url += "Activities?$filter=Id eq Guid'" + d["ActivityId"] + "'";
                    webresponse = Util.GetServiceData(url);
                    string activityname = Util.FindPropertyValue(webresponse, "Name");
                    d.Add("RunbookName", runbookname);
                    d.Add("ActivityName", activityname);

                    result.Add(d);
                }
            }

            // get failed activity instance error messages
            foreach (var d in result)
            {
                url = config.ServiceUrl;
                url += "ActivityInstanceData?$filter=ActivityInstanceId eq Guid'" + d["Id"] + "'";
                url += "and Name eq 'ErrorSummary.Text'";
                webresponse = Util.GetServiceData(url);
                var msg = Util.FindPropertyValue(webresponse, "Value");
                msg = msg.Replace("\n", "");
                msg = msg.Replace("\t", "");

                d.Add("ErrorMessage", msg);
            }

            return result;
        }
    }
}
